package utils

import (
	"fmt"
	"os"

	"github.com/olekukonko/tablewriter"
)

func SubString(str string, byteSize int) string {

	if len(str) <= byteSize {

		return str

	}

	substr := ""

	for _, c := range []rune(str) {

		if byteSize = byteSize - len(string(c)); byteSize < 0 {

			return substr

		}

		substr += string(c)

	}

	return str

}

// FormatFileSize 字节的单位转换 保留两位小数
func FormatFileSize(size int64) string {

	if size < 1024 {
		return fmt.Sprintf("%.2fB", float64(size)/float64(1))
	} else if size < (1024 * 1024) {
		return fmt.Sprintf("%.2fKB", float64(size)/float64(1024))
	} else if size < (1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fMB", float64(size)/float64(1024*1024))
	} else if size < (1024 * 1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fGB", float64(size)/float64(1024*1024*1024))
	} else if size < (1024 * 1024 * 1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fTB", float64(size)/float64(1024*1024*1024*1024))
	} else {
		return fmt.Sprintf("%.2fEB", float64(size)/float64(1024*1024*1024*1024*1024))
	}
}

// FormatFileUint 字节的单位转换 保留两位小数
func FormatFileUint(size uint64) string {

	if size < 1024 {
		return fmt.Sprintf("%.2fB", float64(size)/float64(1))
	} else if size < (1024 * 1024) {
		return fmt.Sprintf("%.2fKB", float64(size)/float64(1024))
	} else if size < (1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fMB", float64(size)/float64(1024*1024))
	} else if size < (1024 * 1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fGB", float64(size)/float64(1024*1024*1024))
	} else if size < (1024 * 1024 * 1024 * 1024 * 1024) {
		return fmt.Sprintf("%.2fTB", float64(size)/float64(1024*1024*1024*1024))
	} else {
		return fmt.Sprintf("%.2fEB", float64(size)/float64(1024*1024*1024*1024*1024))
	}
}

func PrintSearchResult(data [][]string) {

	table := tablewriter.NewWriter(os.Stdout)

	table.SetRowLine(true)
	table.SetAutoMergeCells(true)
	table.SetCenterSeparator("|")
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})

	if len(data) == 0 {
		table.AppendBulk([][]string{{"find", "none"}})
	} else {
		table.SetHeader([]string{"#", "Name", "Size", "Modification Time"})
		table.AppendBulk(data)
	}

	table.Render()
}

func PrintOverview(data [][]string, name string) {

	if len(name) != 0 {
		PrintOverviewWithName(data)
	} else {
		PrintOverviewWithoutName(data)
	}

}

func PrintOverviewWithName(data [][]string) {

	table := tablewriter.NewWriter(os.Stdout)
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	table.SetFooter(data[len(data)-1])
	table.SetHeader([]string{"#", "Name", "Sum Size", "Latest Time"})
	table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
	table.SetCenterSeparator("|")
	table.SetAutoMergeCells(true)
	table.SetRowLine(true)
	table.AppendBulk(data[:len(data)-1])
	table.Render()
}

func PrintOverviewWithoutName(data [][]string) {

	table := tablewriter.NewWriter(os.Stdout)
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	table.SetFooter(data[len(data)-1])
	table.SetHeader([]string{"#", "Category", "Total count", "Sum Size", "Latest Time"})
	table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
	table.SetCenterSeparator("|")
	table.SetAutoMergeCells(true)
	table.SetRowLine(true)
	table.AppendBulk(data[:len(data)-1])
	table.Render()
}

func PrintDupInfo(data [][]string) {
	table := tablewriter.NewWriter(os.Stdout)
	table.SetAlignment(tablewriter.ALIGN_CENTER)
	table.SetFooter(data[len(data)-1])
	table.SetHeader([]string{"#", "Name", "Size"})
	table.SetBorders(tablewriter.Border{Left: true, Top: true, Right: true, Bottom: true})
	table.SetCenterSeparator("|")
	table.SetAutoMergeCells(true)
	table.SetRowLine(true)
	table.AppendBulk(data[:len(data)-1])
	table.Render()
}
